package top.wshape1.shiziapi.mock.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import top.wshape1.shiziapi.common.constant.AuthConstant;
import top.wshape1.shiziapi.common.domain.CommonUser;
import top.wshape1.shiziapi.common.exception.CustomException;
import top.wshape1.shiziapi.common.utils.Base64Util;
import top.wshape1.shiziapi.common.utils.R;
import top.wshape1.shiziapi.mock.domain.Mock;
import top.wshape1.shiziapi.mock.domain.vo.MockVo;
import top.wshape1.shiziapi.mock.service.MockService;

import javax.script.ScriptException;
import java.util.List;

/**
 * @author Wshape1
 * @version 1.0
 * @description
 * @since 2023-10-13
 */

@RestController
@RequestMapping("/mock")
public class MockController {

    @Resource
    private MockService mockService;

    /**
     * 创建Mock
     *
     * @param mock
     * @param userBase64
     * @return
     */
    @PostMapping("/create")
    public R<String> createMock(@RequestBody Mock mock,
                                @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        long mockId = mockService.create(mock, commonUser.getId());

        return R.ok().putData(String.valueOf(mockId));
    }

    /**
     * 更新Mock配置
     *
     * @param mock
     * @param userBase64
     * @return
     */
    @PostMapping("/update")
    public R<Void> updateMock(@RequestBody Mock mock,
                              @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        mockService.update(mock, commonUser.getId());

        return R.ok();
    }

    /**
     * 通过id获取一个Mock配置
     *
     * @param mockId
     * @param userBase64
     * @return
     */
    @GetMapping("/{mockId}")
    public R<Mock> get(@PathVariable String mockId, @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        Mock mock = mockService.getByIdAndUserId(mockId, commonUser.getId());

        return R.ok(mock);
    }

    @PostMapping("/remove")
    public R<Void> removeMock(@RequestParam("id") Long mockId, @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        mockService.remove(mockId, commonUser.getId());

        return R.ok();
    }

    /**
     * 列出用户的所有Mock配置
     *
     * @param userBase64
     * @return
     */
    @GetMapping("/list")
    public R<List<MockVo>> listMock(@RequestParam(value = "keyword", required = false) String keyword,
                                    @RequestHeader(AuthConstant.HEADER_PAYLOAD_NAME) String userBase64) {

        CommonUser commonUser = Base64Util.decodeAndParse(userBase64, CommonUser.class);

        List<MockVo> mockVos = mockService.list(commonUser.getId(), keyword);

        return R.ok(mockVos);
    }

    @RequestMapping("/u/{userId}/{path}")
    public Object mockRequest(@PathVariable String path,
                              @PathVariable Long userId,
                              HttpServletRequest request,
                              HttpServletResponse response) {


        Object obj = null;
        try {
            obj = mockService.mockRequest(userId, "/" + path, request, response);
        } catch (ScriptException e) {
            return R.error("MOCK配置的模板template出现了问题");
        } catch (CustomException e) {
            return e.getR();
        } catch (Exception e) {
            return R.error("出现未知错误");
        }

        return obj;
    }

}
